package com.ocom.human.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.master.EntityMngUserInfo;
import com.ocom.common.entity.master.EntityWechatPerInfo;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.utils.CommonUtil;
import com.ocom.common.utils.StringUtils;
import com.ocom.human.dao.IDaoWechatPerInfo;
import com.ocom.human.service.WechatPerInfoService;
import com.ocom.mysql.aspect.DataSourceAspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class WechatperInfoServiceImpl extends ServiceImpl<IDaoWechatPerInfo, EntityWechatPerInfo> implements WechatPerInfoService {

    @Autowired
    private IDaoWechatPerInfo daoWechatPerInfo;
    /**
     * 获取id
     * @return
     */
    @DataSourceAspect(type = DataSourceEnum.MAIN_DB)
    public List<Long> getCompanyIdsByOpenid(String openid){
        QueryWrapper<EntityWechatPerInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("openid",openid);
        List<EntityWechatPerInfo> list=this.list(queryWrapper);
        if(!list.isEmpty() && list.size()>0){
            List<Long> comIds= list.stream().map(EntityWechatPerInfo -> EntityWechatPerInfo.getComId()).collect(Collectors.toList());
            return comIds;
        }
        return  null;
    }

    @DataSourceAspect(type = DataSourceEnum.MAIN_DB)
    @Override
    public EntityWechatPerInfo saveWxUser(EntityWechatPerInfo entityWechatPerInfo) {
        QueryWrapper<EntityWechatPerInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("com_id",entityWechatPerInfo.getComId());
        queryWrapper.eq("p_id",entityWechatPerInfo.getPId());
        queryWrapper.eq("appid",entityWechatPerInfo.getAppid());
        queryWrapper.last("limit 1");
        EntityWechatPerInfo entityWechatPerInfo2=this.getOne(queryWrapper);
        if(entityWechatPerInfo2!=null){
            UpdateWrapper<EntityWechatPerInfo> updateWrapper=new UpdateWrapper<>();
            updateWrapper.eq("com_id",entityWechatPerInfo.getComId());
            updateWrapper.eq("p_id",entityWechatPerInfo.getPId());
            updateWrapper.eq("appid",entityWechatPerInfo.getAppid());
            updateWrapper.set("openid",entityWechatPerInfo.getOpenid());
            updateWrapper.set("update_time",new Date());
            this.update(updateWrapper);
            return entityWechatPerInfo;
        }
        queryWrapper.eq("openid",entityWechatPerInfo.getOpenid());
        EntityWechatPerInfo entityWechatPerInfo1=this.getOne(queryWrapper);
        if(entityWechatPerInfo1!=null){
            return entityWechatPerInfo;
        }
        this.save(entityWechatPerInfo);
        return entityWechatPerInfo;
    }

    @DataSourceAspect(type = DataSourceEnum.MAIN_DB)
    @Override
    public EntityWechatPerInfo getWxUserByPid(Long companyId, Long pId,String appId) {
        QueryWrapper<EntityWechatPerInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("com_id",companyId);
        queryWrapper.eq("p_id",pId);
        if(!CommonUtil.isNull(appId)){
            queryWrapper.eq("appid",appId);
        }
        queryWrapper.orderByDesc("update_time");
        queryWrapper.last("limit 1");
        return this.getOne(queryWrapper);
    }

    //查找appid、com_id、p_id匹配的记录
    @DataSourceAspect(type = DataSourceEnum.MAIN_DB)
    @Override
    public EntityWechatPerInfo getWechatPerInfo(Long companyId, Long userId, String appid) {
        QueryWrapper<EntityWechatPerInfo> query = Wrappers.query();
        if (StringUtils.isNotNull(companyId)){
            query.eq("com_id",companyId);
        }
        if (StringUtils.isNotNull(userId)){
            query.eq("p_id",userId);
        }
        if (StringUtils.isNotEmpty(appid)){
            query.eq("appid",appid);
        }
        query.last("limit 1");
        return daoWechatPerInfo.selectOne(query);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.MAIN_DB)
    public boolean removePerson(Long companyId, Long pId) {
        System.out.println(companyId);
        System.out.println(pId);
        QueryWrapper<EntityWechatPerInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("com_id",companyId);
        queryWrapper.eq("p_id",pId);
        return this.remove(queryWrapper);
    }

    @DataSourceAspect(type = DataSourceEnum.MAIN_DB)
    @Override
    public EntityWechatPerInfo GetWxPerson(String appId, String openId,Long comId) {
        QueryWrapper<EntityWechatPerInfo> query = Wrappers.query();
        if (comId != null && comId > 0l) {
            query.eq("com_id", comId);
        }
        if (StringUtils.isNotEmpty(appId)){
            query.eq("appid",appId);
        }
        if (StringUtils.isNotEmpty(openId)){
            query.eq("openid",openId);
        }
        query.orderByDesc("update_time");
        query.last("limit 1");
        return daoWechatPerInfo.selectOne(query);
    }

    @DataSourceAspect(type = DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.MASTER)
    @Override
    public void DelWxPerson(String appId, String openId) {
        QueryWrapper<EntityWechatPerInfo> query = Wrappers.query();
        if (StringUtils.isNotEmpty(appId)){
            query.eq("appid",appId);
        }
        if (StringUtils.isNotEmpty(openId)){
            query.eq("openid",openId);
        }
        daoWechatPerInfo.delete(query);
    }


    //查找appid、com_id、p_id匹配的记录
    @Override
    @DataSourceAspect(type = DataSourceEnum.MAIN_DB)
    public List<EntityMngUserInfo> getWxAdminInfo(String appId, String openId) {
        QueryWrapper<EntityWechatPerInfo> query = Wrappers.query();

        return daoWechatPerInfo.getWxAdminInfo(appId,openId);
    }

}
